package com.think.oauth.user.service;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.think.common.utils.Common;
import com.think.oauth.user.dao.user.UserOauthDao;
import com.think.oauth.user.model.UserOauth;

@Service
public class UserOauthService extends ServiceImpl<UserOauthDao, UserOauth> {
	
	public boolean checkUserExits(String identifier,Integer loginType) {
		QueryWrapper<UserOauth> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("identifier", identifier).eq("login_type",
				loginType);
		return this.getOne(queryWrapper) != null;
	}
	
	public UserOauth getUserOauthInfo(Integer userId) {
		QueryWrapper<UserOauth> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("user_id", userId).eq("login_type",
				Common.USER_USERNAME_LOGIN);
		return this.getOne(queryWrapper);
	}

	public UserOauth getUserOauth(String identifier,Integer loginType) {
		QueryWrapper<UserOauth> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("identifier", identifier).eq("login_type",
				loginType);
		return this.getOne(queryWrapper);
	}
	
	public boolean updateUserOauth(String identifier,Integer loginType,String credential) {
		UpdateWrapper<UserOauth> queryWrapper = new UpdateWrapper<>();
		queryWrapper.eq("identifier", identifier).eq("login_type",
				loginType).set("credential", credential);
		return update(queryWrapper);
	}
}
